package com.henu.art.service.Imlp;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.henu.art.entity.AcademicOrigin;
import com.henu.art.entity.Teacher;
import com.henu.art.mapper.AcademicOriginMapper;
import com.henu.art.mapper.TeacherMapper;
import com.henu.art.model.UserDetailModel;
import com.henu.art.service.UserService;
import com.henu.art.util.EncodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements UserService {
    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private AcademicOriginMapper academicOriginMapper;
    @Resource
    private CustomRedis customRedis;

    @Override
    public IPage<Teacher> getAllUser(Integer pageNum, Integer pageSize, Integer deleteStatus) {
        return teacherMapper.selectPage(new Page<>(pageNum, pageSize),
                (new QueryWrapper<Teacher>()).eq("is_delete", deleteStatus)
                        .orderByDesc(deleteStatus == 1, "delete_time")
        );
    }

    @Override
    public int countByEmploymentStatus(String employmentStatus) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employment_status", employmentStatus);
        return (int) this.count(queryWrapper);
    }

    @Override
    public Teacher getUserById(String id) {
        return teacherMapper.selectById(id);
    }

    @Override
    public boolean changePassword(String username, String newPassword) {
        if (username.equals(customRedis.getAdminUsername())) {
            boolean flag = customRedis.setAdminPassword(newPassword);
            customRedis.getAdminPassword();
            return flag;
        }
        Teacher teacher = teacherMapper.selectById(username);
        teacher.setPassword(EncodeUtil.encodePassword(newPassword));
        return teacherMapper.update(teacher, new QueryWrapper<Teacher>().eq("id", username)) > 0;
    }

    @Override
    public List<UserDetailModel> getAllUserDetailInfo() {
        List<Teacher> teachers = teacherMapper.selectList(new QueryWrapper<>());
        return teachers.stream().map(teacher -> {
            AcademicOrigin academicOrigin = academicOriginMapper.selectById(teacher.getId());
            return UserDetailModel.builder()
                    .id(teacher.getId())
                    .name(teacher.getName())
                    .gender(teacher.getGender())
                    .birthDate(teacher.getBirthDate())
                    .entryDate(teacher.getEntryDate())
                    .employmentStatus(teacher.getEmploymentStatus())
                    .unitCode(teacher.getUnitCode())
                    .unitName(teacher.getUnitName())
                    .education(teacher.getEducation())
                    .highestDegree(teacher.getHighestDegree())
                    .academicOrigin(teacher.getAcademicOrigin())
                    .professionalTitle(teacher.getProfessionalTitle())
                    .subjectCategory(teacher.getSubjectCategory())
                    .politicalStatus(teacher.getPoliticalStatus())
                    .nationality(teacher.getNationality())
                    .teachingType(teacher.getTeachingType())
                    .majorName(teacher.getMajorName())
                    .majorCode(teacher.getMajorCode())
                    .password(teacher.getPassword())
                    .teachingYears(teacher.getTeachingYears())
                    .isLabTech(teacher.getIsLabTech())
                    .isDoubleQualified(teacher.getIsDoubleQualified())
                    .hasEngineeringBg(teacher.getHasEngineeringBg())
                    .hasIndustryBg(teacher.getHasIndustryBg())
                    .hasOverseasExp(teacher.getHasOverseasExp())
                    .isOutstandingIdeological(teacher.getIsOutstandingIdeological())
                    .phone(teacher.getPhone())
                    .emergencyContacts(teacher.getEmergencyContacts())
                    .emergencyPhone(teacher.getEmergencyPhone())
                    .idcard(teacher.getIdcard())
                    .shenfen(teacher.getShenfen())
                    .address(teacher.getAddress())
                    .professionalLevel(academicOrigin != null ? academicOrigin.getProfessionalLevel() : null)
                    .zhiwu(academicOrigin != null ? academicOrigin.getZhiwu() : null)
                    .dazhuan(academicOrigin != null ? academicOrigin.getDazhuan() : null)
                    .dazhuanMajor(academicOrigin != null ? academicOrigin.getDazhuanMajor() : null)
                    .benke(academicOrigin != null ? academicOrigin.getBenke() : null)
                    .benkeMajor(academicOrigin != null ? academicOrigin.getBenkeMajor() : null)
                    .benkeLevel(academicOrigin != null ? academicOrigin.getBenkeLevel() : null)
                    .shuoshi(academicOrigin != null ? academicOrigin.getShuoshi() : null)
                    .shuoshiMajor(academicOrigin != null ? academicOrigin.getShuoshiMajor() : null)
                    .shuoshiLevel(academicOrigin != null ? academicOrigin.getShuoshiLevel() : null)
                    .phd(academicOrigin != null ? academicOrigin.getPhd() : null)
                    .phdMajor(academicOrigin != null ? academicOrigin.getPhdMajor() : null)
                    .phdLevel(academicOrigin != null ? academicOrigin.getPhdLevel() : null)
                    .afterphd(academicOrigin != null ? academicOrigin.getAfterphd() : null)
                    .afterphdLevel(academicOrigin != null ? academicOrigin.getAfterphdLevel() : null)
                    .build();
        }).collect(Collectors.toList());
    }

    @Override
    public boolean removeById(String id) {
        // 预防多删可以增加一个redis防抖
        QueryWrapper<Teacher> queryWrapper = (new QueryWrapper<Teacher>()).eq("id", id);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        teacher.setIsDelete(teacher.getIsDelete() + 1);
        teacher.setDeleteTime(String.valueOf(DateUtil.date()));
        return teacherMapper.updateById(teacher) > 0;
    }

    @Override
    public boolean redoDeleteUserById(String id) {
        QueryWrapper<Teacher> queryWrapper = (new QueryWrapper<Teacher>()).eq("id", id);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
//        teacher.setIsDelete(teacher.getIsDelete() - 1);
        teacher.setIsDelete(0);
        teacher.setDeleteTime(null);
        return teacherMapper.updateById(teacher) > 0;
    }
}
